//
//  DrawImaginationView.swift
//  Chatup
//
//  Created by iron man on 2024/1/15.
//

import SwiftUI

struct DrawImaginationView: View {
    @Environment(\.presentationMode) var presentationMode
    @State private var imaginationText: String = ""
    @State var showVIP = false
    @State var isVip = UserDefaults.standard.bool(forKey: "kWhetherVip")
    @State private var showAlert: Bool = false
    @State private var isLoading = false
    
    var body: some View {
        ZStack{
            VStack {
                Spacer().frame(height: 16)
                HStack {
                    Button {
                        if isLoading {return}
                        presentationMode.wrappedValue.dismiss()
                    } label: {
                        Image("nav_close")
                            .resizable()
                            .frame(width: 12, height: 12)
                    }
                    Spacer()
                    
                    if !isVip{
                        Button {
                            showVIP.toggle()
                        } label: {
                            Image("icon_setting_vip_right")
                                .resizable()
                                .frame(width: 77, height: 24)
                        }
                    }

                }.padding(.horizontal, 22)
                    
                Spacer().frame(height: 16)
                    
                Text("🧚" + String.i18nText("feature_draw_imagination"))
                    .font(.system(size: 28).weight(.bold))
                    .foregroundColor(.white)
                Text(String.i18nText("feature_draw_imagination_hint1"))
                    .font(.system(size: 16))
                    .foregroundColor(Color(red: 0.51, green: 0.51, blue: 0.51))
                    .frame(width: 316, height: 71, alignment: .topLeading)
                    
                Spacer().frame(height: 26)
                    
                // 文本输入
                    
                TextView(hint: String.i18nText("feature_draw_imagination_hint2"), text: $imaginationText).padding(.horizontal,23)
                    
                Spacer()

                // 'Draw' 按钮
                Button(action: {
                    isVip = UserDefaults.standard.bool(forKey: "kWhetherVip")
                    if !isVip {
                        showVIP.toggle()
                        return
                    }
                    
                    if isLoading{
                        return
                    }
                    
            
                    let msgModel = MsgModel(type: .user, messageType: .text, message: imaginationText, roleId: 0, isLike: false, isUnlike: false)
                    let imageModel = MsgModel(type: .bot, messageType: .networkImage, message: String.i18nText("chat_wrong"), roleId: 0, isLike: false, isUnlike: false,context: imaginationText)
                    

                    DispatchQueue.main.async {
                        NotificationCenter.default.post(name: Notification.Name("kAppendMessage"), object: ["feature":FeaturesType.drawImage,"msgs":[msgModel, imageModel]])
                        
                        presentationMode.wrappedValue.dismiss()
                    }
                }) {
                    Text(String.i18nText("feature_draw"))
                        .font(.system(size: 18).weight(.bold))
                        .foregroundColor(Color(red: 1, green: 1, blue: 1))
                        .foregroundColor(.clear)
                        .frame(minWidth: 0, maxWidth: .infinity)
                        .frame(height: 57)
                        .background(
                            imaginationText.isEmpty ? Color(red: 0.54, green: 0.54, blue: 0.55) :
                                Color(red: 0.15, green: 0.75, blue: 0.46)
                        )
                        .cornerRadius(12)
                }
                .padding()

                Spacer().frame(height: 32)
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .background(Color.black.opacity(0.8))
            .edgesIgnoringSafeArea(.all)
            .fullScreenCover(isPresented: $showVIP, content: {
                VipView(viewModel: .init(from: "feature_draw_imagination"))
            })
            .onTapGesture {
                NotificationCenter.default.post(name: Notification.Name("kChangeFocused"), object: nil)
            }
            .onReceive(NotificationCenter.default.publisher(for: Notification.Name("refreshVip"))) { _ in
                isVip = UserDefaults.standard.bool(forKey: "kWhetherVip")
            }.alert(isPresented: $showAlert) {
                // 创建Alert实例
                Alert(
                    title: Text(""),
                    message: Text(String.i18nText("chat_no_credit_gpt4.0")),
                    dismissButton: .default(Text(String.i18nText("continue"))) {
                        // 点击好的按钮后的操作
                        
                    }
                )
            }
            
            if isLoading{
                ProgressView()
                    .progressViewStyle(CircularProgressViewStyle(tint: .blue))
                    .scaleEffect(2)
            }
        }
    }
}
